package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.GlobalClobsMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmGlobalClobEntity;
import com.kaili.basic.pojo.entity.MdmStrategyMainEntity;
import com.kaili.basic.pojo.vo.MdmGlobalClobQueryVO;
import com.kaili.basic.pojo.vo.MdmGlobalClobVO;
import com.kaili.basic.service.IGlobalClobService;
import com.kaili.common.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 复选数据 service实现类
 * @author:zhangjl
 * @Time: 2019-07-24
 * @Copyright: ©  杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GlobalClobsServiceImpl extends ServiceImpl<GlobalClobsMapper, MdmGlobalClobEntity> implements IGlobalClobService {

    @Override
    public boolean add(Integer id,MdmStrategyMainSetAddDto dto) {

        List<MdmGlobalClobAddDto> list = dto.getGlobalClobList();
        for (MdmGlobalClobAddDto globalClobDto :list){
            MdmGlobalClobEntity clobEntity1 = new MdmGlobalClobEntity();
            BeanUtils.copyProperties(globalClobDto,clobEntity1);
            clobEntity1.setDataId(id);
            this.insert(clobEntity1);
        }

        return true;
    }

    @Override
    public boolean addApply(Integer id,MdmStrategyMainApplyAddDto dto) {

        List<MdmGlobalClobAddDto> list = dto.getGlobalClobList();
        for (MdmGlobalClobAddDto globalClobDto :list){
            MdmGlobalClobEntity clobEntity1 = new MdmGlobalClobEntity();
            BeanUtils.copyProperties(globalClobDto,clobEntity1);
            clobEntity1.setDataId(id);
            this.insert(clobEntity1);
        }

        return true;
    }

    @Override
    public boolean modify(MdmStrategyMainEntity entity, List<MdmGlobalClobUpdateDto> dtos) {
        //1、删除设备策略
        this.del(entity.getId());
        //2、新增设备类
        for (MdmGlobalClobUpdateDto dto :dtos){
            MdmGlobalClobEntity clobEntity = new MdmGlobalClobEntity();
            BeanUtils.copyProperties(dto,clobEntity);
            clobEntity.setDataId(entity.getId());
            this.insert(clobEntity);
        }

        return true;
    }

    @Override
    public boolean del(Integer id) {
        Wrapper<MdmGlobalClobEntity> wrapper = new EntityWrapper<>();
        wrapper.where("data_id={0}", id);
        this.delete(wrapper);
        return true;
    }

    @Override
    public List<MdmGlobalClobQueryVO> queryStrategyId(MdmGlobalClobQueryDto dto) {
        return baseMapper.queryStrategyId(ObjectUtils.objectToMap(dto));
    }

    @Override
    public List<MdmGlobalClobVO> queryById(MdmGlobalClobQueryDto dto) {
        return baseMapper.queryById(ObjectUtils.objectToMap(dto));
    }

    @Override
    public List<MdmGlobalClobVO> queryList(MdmGlobalClobQueryDto dto) {
        return baseMapper.queryList(ObjectUtils.objectToMap(dto));
    }

}
